package com.biomedica.statemachine;

public class StateMachineEngine implements StateEngineListener{
/*
 * 
 * 
 * This Class will Implement A State MAchine Engine
 * 
 * 
 */
	/** Set The Inital State of The Engine  **/
	public void SetStartState(State s)
	{
		this.CurrentState=s;
		
	}
	/////////////////////////////////////////////////////////
	/**   Get  The State Context **/
	private StateContext Context;	
	public void setContext(StateContext context)
	{
		Context=context;
	}
	/** Set The State Context **/
	public StateContext getContext()
	{
		return Context;
	}
	//////////////////////////////////////////////////////////
	private State CurrentState;
	/////////////////////////////////////////////////////////
	/** The State Engine Constructor **/
	public StateMachineEngine(StateContext context)
	{
		setContext(context);
	}
	////////////////////////////////////////////////////////
	@Override
	public void onChangeState(State nextState) {
		// TODO Auto-generated method stub
		this.CurrentState=nextState;
		this.CurrentState.OnEnter(Context);
		
	}
	///////////////////////////////////////////////////////
	/** Craete a new State Engine and Initalise it Verbs **/
	public static StateMachineEngine Create() {
		// TODO Auto-generated method stub
		StateContext context=new StateContext();
		StateMachineEngine m=new StateMachineEngine(context);
		context.setStateMachineEngine(m);
		context.init();		
		return m;
	}
	/** Run the State Diagram Loop **/
	/////////////////////////////////////////////////////////
	public void Run() throws NoStartStateException {
		// TODO Auto-generated method stub
		if(this.CurrentState!=null)
		{
		this.CurrentState.OnDo(Context);
		this.CurrentState.Test(Context);
		}
		else
			throw new NoStartStateException();
	}
	
	////////////////////////////////////////////////////////////
	/** Set a Verb to the Engine Envirunment **/
	public void set(String name,Object obj)
	{
		this.getContext().getStateEngineEnvirunment().putParameter(name, obj);
	}
	/** Get a Verb from the Envirunment **/
	//////////////////////////////////////////////////////////
	public Object get(String name)
	{
		return this.getContext().getStateEngineEnvirunment().getParameter(name);
	}
	/** Add a new Parameter to The Envirunmet **/
	//////////////////////////////////////////////////////////
	public void AddParameter(String string, Object b) {
		// TODO Auto-generated method stub
		this.getContext().put(string, b);
		
	}
	/** get The Parameter Verb similer to get **/
	////////////////////////////////////////////////////////
	public Object GetParameter(String name)
	{
		return getContext().get(name);
	}
}
